﻿/*
 * Created by SharpDevelop.
 * User: 김효순
 * Date: 2012-08-31
 * Time: 오후 5:52
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
//using System.Management;
using System.ServiceProcess;

namespace 서비스제어
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	/// 
	
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void Bt1Click(object sender, EventArgs e)
		{
			ServiceController[] serviceControllers = ServiceController.GetServices();
			foreach(ServiceController serviceController in serviceControllers) {
				string svrName = serviceController.ServiceName;
				string dspName = serviceController.DisplayName;
				string status = "";
				switch(serviceController.Status) {
					case ServiceControllerStatus.Paused:
						status = "Paused";
						break;
					case ServiceControllerStatus.PausePending:
						status = "PausePending";
						break;
					case ServiceControllerStatus.Running:
						status = "Running";
						break;
					case ServiceControllerStatus.StartPending:
						status = "StartPending";
						break;
					case ServiceControllerStatus.Stopped:
						status = "Stopped";
						break;
					case ServiceControllerStatus.StopPending:
						status = "StopPending";
						break;
				}
				
				if(!tb1.Text.Equals("")) tb1.Text += "\r\n";
				tb1.Text += svrName + " - " + dspName + " - " + status;
			}
			
			
			
//			ManagementClass wmiClass = null;
//			ManagementObjectCollection wmiServices = null;
//			
//			try{
//				wmiClass = new ManagementClass("Win32_Service");
//				wmiServices = wmiClass.GetInstances();
//				
//				foreach(ManagementObject wmiService in wmiServices){
//					string curName = (string) wmiService.Properties["Name"].Value;
//					string dspName = (string) wmiService.Properties["DisplayName"].Value;
//					
//					if(!tb1.Text.Equals("")) tb1.Text += "\n";
//					tb1.Text += curName + " - " + dspName;
//				}
//			} catch (Exception ex){
//				MessageBox.Show("예외발생: " + ex.ToString());
//			} finally {
//				if(wmiServices != null) wmiServices.Dispose();
//				if(wmiClass != null) wmiClass.Dispose();
//			}
		}
		
		void Bt2Click(object sender, EventArgs e)
		{
			try {
				using (ServiceController serviceController = new ServiceController(tb2.Text))
				{
					if(serviceController.Status == ServiceControllerStatus.Stopped) serviceController.Start();
				}
			} catch (System.Exception ex) {
				MessageBox.Show("Start 실패: " + ex.Message);
			}
		}
		
		void Bt3Click(object sender, EventArgs e)
		{
			try {
				using (ServiceController serviceController = new ServiceController(bt2.Text)) {
					if(serviceController.Status == ServiceControllerStatus.Running) serviceController.Stop();
				}
			} catch (System.Exception ex) {
				MessageBox.Show("Stop 실패: " + ex.Message);
			}
		}
		
		void Bt4Click(object sender, EventArgs e)
		{
			try {
				using (ServiceController serviceController = new ServiceController(bt2.Text)) {
					if(serviceController.Status == ServiceControllerStatus.Running) serviceController.Pause();
				}
			} catch (System.Exception ex) {
				MessageBox.Show("Pause 실패: " + ex.Message);
			}
		}
		
		void Bt5Click(object sender, EventArgs e)
		{
			try {
				using (ServiceController serviceController = new ServiceController(bt2.Text)) {
					if(serviceController.Status == ServiceControllerStatus.Paused) serviceController.Continue();
				}
			} catch (System.Exception ex) {
				MessageBox.Show("Continue 실패: " + ex.Message);
			}
		}
	}
}
